import Vue from 'vue'
import App from './App.vue'
import {
  Button, Radio, Container, Main, Header, Aside, Menu, Submenu, MenuItem, MenuItemGroup, Dropdown,
  DropdownMenu, DropdownItem, Row, Col, Card, Table, TableColumn, Breadcrumb, BreadcrumbItem, Tag, Form, FormItem, Dialog, Input, Select, Switch, DatePicker, Option, Pagination,
  MessageBox, Message, Descriptions, DescriptionsItem, Upload, Image, Drawer, Loading
} from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './assets/less/index.less'

import router from './router'
import store from './store'
import http from 'axios'

Vue.config.productionTip = false
Vue.use(Button)
Vue.use(Radio)
Vue.use(Container),
  Vue.use(Main),
  Vue.use(Header),
  Vue.use(Aside),
  Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItem)
Vue.use(MenuItemGroup)
Vue.use(Dropdown)
Vue.use(DropdownMenu)
Vue.use(DropdownItem)
Vue.use(Row)
Vue.use(Col)
Vue.use(Card)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(Breadcrumb)
Vue.use(BreadcrumbItem)
Vue.use(Tag)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Select)
Vue.use(Option)
Vue.use(Switch)
Vue.use(DatePicker)
Vue.use(Dialog)
Vue.use(TableColumn)
Vue.use(Pagination)
Vue.use(Descriptions)
Vue.use(DescriptionsItem)
Vue.use(Upload)
Vue.use(Image)
Vue.use(Drawer)
Vue.use(Loading)

Vue.prototype.$http = http
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$prompt = MessageBox.prompt
Vue.prototype.$message = Message

//全局路由守卫 为什么写在这里不写在路由的index.js中 
router.beforeEach((to, from, next) => {
  store.commit('getToken') //通过vuex从别的组件中获取道token
  const token = store.state.user.token //拿到token
  if (!token && to.name !== 'login') {
    next({
      name: 'login'
    }) //对登录进行管理 如果不是从login进入路由 或者没有token则直接next道login组件中 也就是开始界面
  }
  else if (token && to.name === 'login') {
    next({ name: 'home' }) //TOKEN验证成功则进入home页面 
  }
  else {
    next() //如果已经保存了token则直接进入Home页面
  }
})

new Vue({
  store,
  router,
  render: h => h(App),
  created() {
    store.commit('addMenu', router)
  }
}).$mount('#app')
